package cn.fbs.controller;

import cn.fbs.enums.MediaEnum;
import cn.fbs.model.TDataInfo;
import cn.fbs.model.TImageInfo;
import cn.fbs.model.TVideoInfo;
import cn.fbs.param.*;
import cn.fbs.service.DataService;
import cn.fbs.service.ImageDataService;
import cn.fbs.service.UploadService;
import cn.fbs.vo.*;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Base64;
import java.util.List;

@Api(tags = "图像资料管理")
@RestController
//@RequestMapping(value = {"imageData","/undefined/imageData/"})
@RequestMapping(value = "imageData")
public class ImageDataController {

    @Autowired
    private ImageDataService imageDataService;

    @Autowired
    private DataService dataService;


    @ApiOperation("查询目标下的图像,视频,资料")
    @PostMapping("getTargetData")
    public Object getTargetData(@RequestBody
                                @ApiParam(name = "params",
                                        value = "目标主键数组")
                                        ListParamsVo params, HttpServletRequest request) {
        Long userId = getUserId(request);
        params.setUserId(userId);
        return imageDataService.getTargetData(params);
    }

    @ApiOperation("查询图像,视频,资料关联标签后的信息")
    @GetMapping("SingleInfo")
    public Object SingleInfo(Integer type, Long id) {
        return imageDataService.SingleInfo(type, id);
    }


    @ApiOperation("编辑资料文档")
    @PostMapping("updateDataInfo")
    public void updateDataInfo(@RequestBody DataParam uploadParam, HttpServletRequest request) {
        uploadParam.setIsExamine(null);
        uploadParam.setOperator(getUserName(request));
        imageDataService.updateDataInfo(uploadParam);
    }

    @ApiOperation("根据文件名词搜索当前目标下的资料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "targetType", value = "目标序号"),
            @ApiImplicitParam(name = "dataName", value = "资料名称")}
    )
    @GetMapping("searchData")
    public List<TDataInfoVo> searchData(String targetType, String dataName) {
        return imageDataService.searchData(targetType, dataName);
    }


    @ApiOperation("资料文档详情预览")
    @ApiImplicitParam(name = "dataInfoId", value = "资料文档Id")
    @GetMapping("dataPreview")
    public String dataPreview(Long dataInfoId) {

        return imageDataService.dataPreview(dataInfoId);
    }

//    @ApiOperation("根据Id分享资料文档")
//    @ApiImplicitParam(name = "dataInfoId", value = "资料文档Id")
//    @GetMapping("shareData")
//    public String shareData(Long dataInfoId) throws IOException {
//        return imageDataService.shareData(dataInfoId);
//    }

//    @ApiOperation("分享目标下的多个图像、视频或资料")
//    @PostMapping("shareZCData")
//    public String shareZCData(@RequestBody ShareParam param) throws IOException {
//        if (param.getImageIds()==null&&param.getDataIds()==null&&param.getVideoIds()==null){
//            return "请输入要分享的文件id";
//        }
//        return imageDataService.shareZCData(param);
//    }

//    @ApiOperation("北京-分享目标下的多个图像")
//    @GetMapping("shareImage")
//    public Object shareImage( @RequestParam("ids") ArrayList<Long> ids) throws IOException {
//        return imageDataService.shareImage(ids);
//    }
//    @ApiOperation("北京-分享目标下的多个视频")
//    @GetMapping("shareVideo")
//    public Object shareVideo( @RequestParam("ids") ArrayList<Long> ids) throws IOException {
//        return imageDataService.shareVideo(ids);
//    }


//    @ApiOperation("北京-分享目标下的多个图像")
//    @PostMapping("shareData")
//    public String shareData(@RequestBody ShareParam param) throws IOException {
//        if (param.getImageIds()==null&&param.getDataIds()==null&&param.getVideoIds()==null){
//            return "请输入要分享的文件id";
//        }
//        return imageDataService.shareData(param);
//    }


    @ApiOperation("根据id和类型删除图像、视频、资料")
    @PostMapping("delete")
    public void deleteData(@RequestBody() DeleteParam params) {
        imageDataService.deleteData(params);
    }

    @ApiOperation("获得固化标签")
    @GetMapping("/memo")
    public Object fixedMemo() {
        return imageDataService.fixedMemo();
    }


    private String getUserName(HttpServletRequest request) {
        String sessionId = request.getHeader("sessionId");
        sessionId = new String(Base64.getDecoder().decode(sessionId));
        JSONObject jsonObject = JSONObject.parseObject(sessionId);
        String userName = jsonObject.get("userName").toString();
        return userName;
    }

    private Long getUserId(HttpServletRequest request) {
        String sessionId = request.getHeader("sessionId");
        sessionId = new String(Base64.getDecoder().decode(sessionId));
        JSONObject jsonObject = JSONObject.parseObject(sessionId);
        String userId = jsonObject.get("userId").toString();
        return Long.parseLong(userId);
    }


    @ApiOperation("ZC文件基本详情")
    @GetMapping("/ZCDataItem")
    @ApiImplicitParam(name = "id", value = "文档ID")
    public TDataInfo getDataItem(@RequestParam("id") Long id) {
        return dataService.getDataItem(id);
    }


    @ApiOperation("生成文件名称")
    @PostMapping("/generatorFileName")
    @ApiImplicitParam(name = "list", value = "标签id数组")
    public String generatorFileName(@RequestBody ListParam listParam) {
        String fileName = imageDataService.generatorFileName(listParam.getList(), listParam.getZzdx(), listParam.getDataType());
        if (fileName.startsWith("500")) {
            return fileName;
        }
        return 200 + "," + fileName;
    }

    @ApiOperation("目标关联数据")
    @PostMapping("/targetJoinData")
    public String targetJoinData(@RequestBody targetJoinDataParam param) {
        return imageDataService.targetJoinData(param);
    }

    @ApiOperation("目标同步")
    @PostMapping("/synTarget")
    public String synTarget() {
        return imageDataService.synTarget();
    }

    @PostMapping("archive1")
    public Object archive1(@RequestBody String json) {
        return json;
    }

    @ApiOperation("根据id和类型删除图像、视频、资料对应文件(未上传时)")
    @PostMapping("onlyDeleteFile")
    public void onlyDeleteFile(@RequestParam("fileUrl") String fileUrl) {
        imageDataService.onlyDeleteFile(fileUrl);
    }


}
